<template>
    <!-- 出库订单审核弹窗 -->
    <div class="dialog-box">
        <el-dialog v-model="open" title="出库订单审核" width="1000" :close-on-click-modal="false">
            <!-- 订单信息 -->
            <div class="order-info">
                <div class="info-list">
                    <div class="info-item">
                        <span>出库单号</span>
                        <span>{{ form.order_no }}</span>
                    </div>
                    <div class="info-item">
                        <span>下单日期</span>
                        <span>{{ form.order_date }}</span>  
                    </div>
                    <div class="info-item">
                        <span>交货日期</span>
                        <span>{{ form.order_date }}</span>
                    </div>
                    <div class="info-item">
                        <span>客户</span>
                        <span>{{ form.customer_name }}</span>
                    </div>
                    <div class="info-item">
                        <span>提交人</span>
                        <span>{{ form.creator_name }}</span>
                    </div>
                    <div class="info-item">
                        <span>提交时间</span>
                        <span>{{ form.created_at }}</span>
                    </div>
                </div>
            </div>

            <!-- 出库产品 -->
            <div class="product-section">
                <div class="section-title">出库产品</div>
                <div class="table-container">
                    <el-table :data="tableData" border style="width: 100%">
                        <el-table-column prop="name" label="产品名称" align="center" />
                        <el-table-column prop="specification" label="规格型号" align="center" />
                        <el-table-column prop="measure_unit_name" label="单位" align="center" />
                        <el-table-column prop="num" label="出库数量" align="center" />
                    </el-table>
                </div>
            </div>

            <!-- 审核意见 -->
            <div class="audit-section">
                <div class="section-title">审核意见</div>
                <el-form :model="form" label-width="0">
                    <el-form-item>
                        <el-input v-model="form.opinion" type="textarea" placeholder="请输入你的备注" :rows="4" />
                    </el-form-item>
                </el-form>
            </div>

            <template #footer>
                <div class="dialog-footer">
                    <el-button @click="open = false">取消</el-button>
                    <el-button @click="handleReject" plain>审核拒绝</el-button>
                    <el-button type="primary" color="#626aef" @click="handleApprove">审核通过</el-button>
                </div>
            </template>
        </el-dialog>
    </div>
</template>

<script setup lang="ts">
import { ref } from "vue";
import { ElMessage } from "element-plus";
import api from '@/api';

// 定义事件
const emit = defineEmits(['refresh']);

// 定义订单表单接口
interface OrderForm {
    order_no?: string;
    order_date?: string;
    delivery_date?: string;
    customer_name?: string;
    creator_name?: string;
    created_at?: string;
    opinion?: string;
    [key: string]: any; // 允许其他属性
}

// 定义产品数据接口
interface ProductData {
    id?: number;
    name?: string;
    specification?: string;
    measure_unit_name?: string;
    num?: string | number;
    [key: string]: any;
}

const open = ref<boolean>(false);
const form = ref<OrderForm>({
    order_no: '',
    order_date: '',
    delivery_date: '',
    customer_name: '',
    creator_name: '',
    created_at: '',
    opinion: '同意'
});

// 表格内部的数据
const tableData = ref<ProductData[]>([]);

// 打开弹窗
const openLoading = (item: any) => {
    console.log('接收到的审核数据:', item);
    
    if (item) {
        // 填充表单数据
        form.value = { 
            ...item,
            opinion: '' // 清空审核意见，让用户手动输入
        };
        
        // 提取并处理产品数据
        let extractedProducts: ProductData[] = [];
        
        if (item.spu_data && Array.isArray(item.spu_data)) {
            item.spu_data.forEach((spuItem: any) => {
                console.log('处理SPU项:', spuItem);
                
                if (spuItem.product_data && Array.isArray(spuItem.product_data)) {
                    spuItem.product_data.forEach((productItem: any) => {
                        console.log('处理产品项:', productItem);
                        
                        // 构造表格显示的产品数据
                        const product: ProductData = {
                            id: productItem.id,
                            name: productItem.name,
                            specification: productItem.specification,
                            measure_unit_name: productItem.open_unit_name,
                            num: productItem.num
                        };
                        
                        extractedProducts.push(product);
                    });
                }
            });
        }
        
        tableData.value = extractedProducts;
        console.log('最终表格数据:', tableData.value);
    }
    
    open.value = true;
};

// 审核通过
const handleApprove = async () => {
    console.log('执行审核通过，订单ID:', form.value.id);
    
    try {
        const res = await api.Outbound.audit({
            id: form.value.id,
            audit_note: form.value.opinion || '同意',
            status: 2 // 2=通过
        });
        
        console.log('审核通过结果:', res);
        
        if (res.code === 200) {
            ElMessage.success('审核通过成功');
            open.value = false;
            // 通知父组件刷新列表
            emit('refresh');
        } else {
            ElMessage.error(res.message || '审核失败，请重试');
        }
    } catch (error) {
        console.error('审核通过时发生错误:', error);
        ElMessage.error('审核失败，请重试');
    }
};

// 审核拒绝
const handleReject = async () => {
    console.log('执行审核拒绝，订单ID:', form.value.id);
    
    if (!form.value.opinion || form.value.opinion.trim() === '' || form.value.opinion === '同意') {
        ElMessage.warning('审核拒绝时请填写具体的拒绝原因');
        return;
    }
    
    try {
        const res = await api.Outbound.audit({
            id: form.value.id,
            audit_note: form.value.opinion,
            status: 3 // 3=拒绝
        });
        
        console.log('审核拒绝结果:', res);
        
        if (res.code === 200) {
            ElMessage.success('审核拒绝成功');
            open.value = false;
            // 通知父组件刷新列表
            emit('refresh');
        } else {
            ElMessage.error(res.message || '审核失败，请重试');
        }
    } catch (error) {
        console.error('审核拒绝时发生错误:', error);
        ElMessage.error('审核失败，请重试');
    }
};

defineExpose({
    openLoading,
});
</script>

<style lang="scss" scoped>
::v-deep .el-dialog {
    --el-dialog-padding-primary: 0px;
}

::v-deep .el-dialog__header {
    margin: 0;
    padding: 14px 20px;
    border-bottom: 1px solid #f0f0f0;

    .el-dialog__title {
        font-size: 16px;
        font-weight: 500;
    }
}

::v-deep .el-dialog__body {
    padding: 20px;
}

::v-deep .el-dialog__footer {
    padding: 10px 20px;
    border-top: 1px solid #f0f0f0;
}

.order-info {
    background: #f8f9fa;
    border-radius: 2px;
    padding: 16px 20px;
    margin-bottom: 24px;

    .info-list {
        display: grid;
        grid-template-columns: repeat(4, 1fr);
        gap: 16px;

        .info-item {
            display: flex;
            align-items: center;

            span:first-child {
                color: #909399;
                margin-right: 8px;
                white-space: nowrap;
            }

            span:last-child {
                color: #303133;
            }
        }
    }
}

.section-title {
    font-size: 14px;
    font-weight: 500;
    color: #303133;
    margin-bottom: 16px;
    display: flex;
    align-items: center;

    &::before {
        content: '';
        width: 2px;
        height: 14px;
        background: #626aef;
        margin-right: 8px;
        border-radius: 1px;
    }
}

.product-section {
    margin-bottom: 24px;

    .table-container {
        margin-bottom: 16px;
    }

    .table-summary {
        display: flex;
        justify-content: space-between;
        gap: 32px;
        padding: 8px 16px;
        background: #f8f9fa;
        border-radius: 2px;

        .summary-item {
            display: flex;
            align-items: center;
            gap: 15px;
        }
    }
}

.attachment-section {
    margin-bottom: 24px;

    .attachment-list {
        display: flex;
        flex-direction: column;
        gap: 12px;

        .attachment-item {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 12px 16px;
            background: #f8f9fa;
            border-radius: 2px;

            .file-info {
                display: flex;
                align-items: center;
                gap: 8px;

                .file-icon {
                    width: 24px;
                    height: 24px;
                }

                .file-name {
                    color: #303133;
                }

                .file-size {
                    color: #909399;
                    font-size: 12px;
                }
            }

            .file-actions {
                display: flex;
                align-items: center;
                gap: 8px;

                .action-btn {
                    color: #626aef;
                    cursor: pointer;

                    &:hover {
                        color: #4318ff;
                    }
                }

                .action-divider {
                    color: #dcdfe6;
                }
            }
        }
    }
}

.audit-section {
    .el-textarea {
        --el-input-bg-color: #f8f9fa;
    }
}

:deep(.el-table) {
    --el-table-header-bg-color: #f8f9fa;
    --el-table-border-color: #ebeef5;
    --el-table-header-text-color: #606266;
}

:deep(.el-button--primary) {
    --el-button-hover-bg-color: #4318ff;
    --el-button-hover-border-color: #4318ff;
}
</style>
